<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <!-- <el-form-item label="用户ID" prop="memberId" >
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="回收烟/条盒种类ID" prop="yhDetailsId">
        <el-input
          v-model="queryParams.yhDetailsId"
          placeholder="请输入回收烟/条盒种类ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="烟/条盒名称" prop="yhName">
        <el-input
          v-model="queryParams.yhName"
          placeholder="请输入烟/条盒名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="烟/条盒条码" prop="barCode">
        <el-input
          v-model="queryParams.barCode"
          placeholder="请输入烟/条盒条码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="防伪编码" prop="antifakeCode">
        <el-input
          v-model="queryParams.antifakeCode"
          placeholder="请输入防伪编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="积分" prop="point">
        <el-input
          v-model="queryParams.point"
          placeholder="请输入积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" clearable placeholder="请选择状态">
          <el-option
            v-for="item in stateOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="回收类型" prop="yhType">
        <el-select v-model="queryParams.yhType" clearable placeholder="请选择回收类型">
          <el-option
            v-for="item in resOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="备注" prop="remake">
        <el-input
          v-model="queryParams.remake"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yh:scanCodeRecord:export']"
        >导出</el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-check" 
          size="mini"
          @click="handleprocess"
        >批量通过</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleReject"
        >批量拒绝</el-button>

      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yh:scanCodeRecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yh:scanCodeRecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yh:scanCodeRecord:remove']"
        >删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yh:scanCodeRecord:export']"
        >导出</el-button>
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"  :selectable="selected"/>
      <!-- <el-table-column label="id" align="center" prop="id" width="180" /> -->
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="类型" align="center" prop="yhTypeText" />
      <el-table-column label="烟/条盒名称" align="center" prop="yhName" />
      <el-table-column label="烟/条盒条码" align="center" prop="barCode" />
      <el-table-column label="二维码" align="center" prop="qrCode" />
      <el-table-column label="防伪编码" align="center" prop="antiFakeCode" />
      <el-table-column label="积分" align="center" prop="point" />
      <el-table-column label="状态" align="center" prop="statusText" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <!-- <el-table-column label="状态" align="center" key="state" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.state"
            active-value="2"
            inactive-value="3"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="备注" align="center" prop="remake" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button 
            v-if="scope.row.state == '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yh:scanCodeRecord:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.state != '1'"
          >暂无</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yh:scanCodeRecord:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户烟/条盒扫码记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="用户ID" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入用户ID" />
        </el-form-item> -->
        <!-- <el-form-item label="回收烟/条盒种类ID" prop="yhDetailsId">
          <el-input v-model="form.yhDetailsId" placeholder="请输入回收烟/条盒种类ID" />
        </el-form-item> -->
        <!-- <el-form-item label="烟/条盒名称" prop="yhName">
          <el-input v-model="form.yhName" placeholder="请输入烟/条盒名称" />
        </el-form-item>
        <el-form-item label="烟/条盒条码" prop="barCode">
          <el-input v-model="form.barCode" placeholder="请输入烟/条盒条码" />
        </el-form-item>
        <el-form-item label="防伪编码" prop="antifakeCode">
          <el-input v-model="form.antifakeCode" placeholder="请输入防伪编码" />
        </el-form-item>
        <el-form-item label="积分" prop="point">
          <el-input v-model="form.point" placeholder="请输入积分" />
        </el-form-item> -->
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" clearable placeholder="请选择状态">
            <el-option
              v-for="item in stateOptionsnew"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <!-- <el-input v-model="form.statusText" placeholder="请输入状态" /> -->
        </el-form-item>
        <!-- <el-form-item label="备注" prop="remake">
          <el-input v-model="form.remake" placeholder="请输入备注" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord,processRecord,batchProcess } from "@/api/yh/scanCodeRecord";

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户烟/条盒扫码记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: null,
        // yhDetailsId: null,
        yhName: null,
        barCode: null,
        antifakeCode: null,
        point: null,
        state: null,
        remake: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        memberId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        // yhDetailsId: [
        //   { required: true, message: "回收烟/条盒种类ID不能为空", trigger: "blur" }
        // ],
      },
      stateOptions:[
        {
          value:1,
          label:'待审核'
        },
        {
          value:2,
          label:'审核通过'
        },
        {
          value:3,
          label:'审核失败'
        },
      ],
      resOptions:[
        {
          value:1,
          label:'烟盒'
        },
        {
          value:2,
          label:'条盒'
        }
      ],
      stateOptionsnew:[
        {
          value:2,
          label:'审核通过'
        },
        {
          value:3,
          label:'审核失败'
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    selected(row, index) {
      console.log()
      if (row.state != 1) {
        return false //不可勾选
      } else {
        return true; //可勾选
      }
    },
    // 用户状态修改
    handleStatusChange(row) {
      console.log(row.id,'row',row.state)
      let text = row.state === "2" ? "停用" : "通过";
      this.$modal.confirm('确认要"' + text + '""' + row.yhName + '"的扫码信息吗？').then(function() {
        return processRecord(row.id, row.state);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.state = row.state === "2" ? "3" : "2";
      });
    },
    /** 查询用户烟/条盒扫码记录列表 */
    getList() {
      this.loading = true;
      // console.log(this.queryParams,'this.queryParams')
      listRecord(this.queryParams).then(response => {
        let text = ['待审核','审核通过','审核失败']
        let t = ['烟盒','条盒']
        this.recordList = response.rows.map(item =>{
          return{
            ...item,
            statusText: text[item.state-1],
            yhTypeText: t[item.yhType-1]
          }
        });
        // console.log(this.recordList,'this.recordList')
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberId: null,
        // yhDetailsId: null,
        yhName: null,
        barCode: null,
        antifakeCode: null,
        point: null,
        state: null,
        remake: null,
        createTime: null,
        updateTime: null,
        updateBy: null,
        yhType:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户烟/条盒扫码记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        this.form.state = ''
        this.open = true;
        this.title = "修改用户烟/条盒扫码记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form,'1111')
      processRecord({
        id:this.form.id,
        state:this.form.state
      }).then(response => {
        console.log(response,'responseresponse')
        if(response.code == 200){
          this.$modal.msgSuccess(response.msg);
          this.open = false;
          this.getList();
        }
      })
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != null) {
      //       updateRecord(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
            
      //       this.form.statusText == '审核中'? this.form.state = 1:(this.form.statusText == '审核通过'?this.form.state = 2:3)

      //       console.log(this.form,'this.form')
      //       addRecord(this.form).then(response => {
              
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户烟/条盒扫码记录编号为"' + ids + '"的数据项？').then(function() {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yh/scanCode/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    },
    // 批量通过
    handleprocess(row){
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认批量通过？').then(function() {
        return batchProcess({
          ids,
          state:'2'
        });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
     // 批量拒绝
     handleReject(row){
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认批量拒绝？').then(function() {
        return batchProcess({
          ids,
          state:'3'
        });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    }
  }
};
</script>
